// # Copyright (C) 2025 All rights reserved.
// #   
// # @File social_login_key.js
// # @Brief 
// # @Author 杨嘉祥, vnimy@mediad.cn
// # @Version 1.0
// # @Date 2025-01-26
// #
frappe_wxwork.SocialLoginKeyController = class SocialLoginKeyController extends frappe.ui.form.Controller {
  is_wxwork() {
    this.frm.set_value({
      icon: '/assets/frappe_wxwork/images/WWLogo.svg',
      base_url: 'https://qyapi.weixin.qq.com',
      authorize_url: 'https://login.work.weixin.qq.com/wwlogin/sso/login',
      access_token_url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
      redirect_url: '/api/method/frappe_wxwork.oauth2_logins.login_via_wxwork',
      custom_base_url: false,
      auth_url_data: JSON.stringify({
        "login_type": "CorpApp",
        "appid": this.frm.doc.client_id,
        "agentid": this.frm.doc.wxwork_agentid,
      }),
      user_id_property: 'userid',
    });
  }

  client_id() {
    if (this.frm.doc.is_wxwork) {
      let auth_url_data = {};
      try {
        auth_url_data = JSON.parse(this.frm.doc.auth_url_data)
      } catch (e) {

      }
      auth_url_data.appid = this.frm.doc.client_id;
      this.frm.set_value('auth_url_data', JSON.stringify(auth_url_data));
    }
  }

  wxwork_agentid() {
    if (this.frm.doc.is_wxwork) {
      let auth_url_data = {};
      try {
        auth_url_data = JSON.parse(this.frm.doc.auth_url_data)
      } catch (e) {

      }
      auth_url_data.agentid = this.frm.doc.wxwork_agentid;
      this.frm.set_value('auth_url_data', JSON.stringify(auth_url_data));
    }
  }
}

extend_cscript(cur_frm.cscript, new frappe_wxwork.SocialLoginKeyController({ frm: cur_frm }));